import type { MockConfig, MockMethod } from "vite-plugin-mock";

const loginUserName = "admin";
const loginPassWord = "123456";

const login = (): MockMethod => {
  return {
    url: "/user/token",
    method: "post",
    timeout: 4000,
    response: ({ body }) => {
      console.log("登录信息", body);
      const { username, password } = body;
      if (loginUserName == username && loginPassWord == password) {
        return {
          code: 0,
          message: "ok",
          data: {
            token: "123456",
            expirationTime: new Date(),
          },
        };
      }
      return {
        code: 401,
        message: "账号密码错误",
      };
    },
  };
};

const getUserInfo = (): MockMethod => {
  let user1 = {
    userId: "1",
    username: "admin",
    role: "admin",
    permissions: ["admin"],
  };
  return {
    url: "/user/info",
    method: "get",
    response: () => {
      return {
        code: 0,
        message: "ok",
        data: user1,
      };
    },
  };
};

export default (config?: MockConfig) => {
  return [login(), getUserInfo()] as MockMethod[];
};
